package com.example.x6.configurationmaintenance.Module;

import android.content.Context;
import android.util.Log;
import com.example.x6.configurationmaintenance.Interface.LogListener.OnDmesgListener;
import com.example.x6.configurationmaintenance.Interface.LogListener.OnLogSyncListener;
import com.example.x6.configurationmaintenance.Interface.LogListener.OnLogradioSyncListener;
import com.example.x6.configurationmaintenance.Utils.SuCommand;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Iterator;

public class LogUtil {
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private OnDmesgListener onDmesgListener;
    private OnLogSyncListener onLogSyncListener;
    private OnLogradioSyncListener onLogradioSyncListener;
    private SuCommand suCommand;

    public LogUtil(Context context2) {
        this.context = context2;
        this.suCommand = new SuCommand();
    }

    public void setOnDmesgListener(OnDmesgListener onDmesgListener2) {
        this.onDmesgListener = onDmesgListener2;
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogUtil.AnonymousClass1 */

            public void run() {
                LogUtil.this.regiestDemsgListener();
            }
        }).start();
    }

    public void setOnLogradioSyncListener(OnLogradioSyncListener onLogradioSyncListener2) {
        this.onLogradioSyncListener = onLogradioSyncListener2;
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogUtil.AnonymousClass2 */

            public void run() {
                LogUtil.this.regiestLogradioListener();
            }
        }).start();
    }

    public void setOnLogSyncListener(OnLogSyncListener onLogSyncListener2) {
        this.onLogSyncListener = onLogSyncListener2;
        new Thread(new Runnable() {
            /* class com.example.x6.configurationmaintenance.Module.LogUtil.AnonymousClass3 */

            public void run() {
                LogUtil.this.regiestLogListener();
            }
        }).start();
    }

    private void killProgress(String progressName) {
        SuCommand suCommand2 = this.suCommand;
        Iterator<String> it = suCommand2.execRootCmd("ps | grep " + progressName).iterator();
        while (it.hasNext()) {
            String string = it.next();
            Log.e(this.TAG, string);
            String[] pid = string.trim().split("\\s+");
            SuCommand suCommand3 = this.suCommand;
            suCommand3.execRootCmdSilent("kill " + pid[1]);
        }
    }

    public void stopDmesgListener() {
        killProgress("dmesg");
        this.onDmesgListener = null;
    }

    public void stopLogcatRadioListener() {
        killProgress("logcat");
        this.onLogradioSyncListener = null;
    }

    public void stopLogcatListener() {
        killProgress("logcat");
        this.onLogSyncListener = null;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void regiestDemsgListener() {
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream dos2 = new DataOutputStream(p.getOutputStream());
            DataInputStream dis2 = new DataInputStream(p.getInputStream());
            dos2.writeBytes("dmesg\n");
            dos2.flush();
            dos2.writeBytes("exit\n");
            dos2.flush();
            while (true) {
                String line = dis2.readLine();
                if (line == null) {
                    break;
                }
                this.onDmesgListener.dmesg(line);
            }
            p.waitFor();
            if (dos2 != null) {
                try {
                    dos2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dis2 != null) {
                try {
                    dis2.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        } catch (Exception e3) {
            e3.printStackTrace();
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Throwable th) {
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e5) {
                    e5.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e6) {
                    e6.printStackTrace();
                }
            }
            throw th;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void regiestLogListener() {
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream dos2 = new DataOutputStream(p.getOutputStream());
            DataInputStream dis2 = new DataInputStream(p.getInputStream());
            dos2.writeBytes("logcat -v time\n");
            dos2.flush();
            dos2.writeBytes("exit\n");
            dos2.flush();
            System.currentTimeMillis();
            while (true) {
                String line = dis2.readLine();
                if (line == null) {
                    break;
                }
                this.onLogSyncListener.logSync(line);
                mDelay(10);
            }
            p.waitFor();
            if (dos2 != null) {
                try {
                    dos2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dis2 != null) {
                try {
                    dis2.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        } catch (Exception e3) {
            e3.printStackTrace();
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Throwable th) {
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e5) {
                    e5.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e6) {
                    e6.printStackTrace();
                }
            }
            throw th;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void regiestLogradioListener() {
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            Process p = Runtime.getRuntime().exec("su");
            DataOutputStream dos2 = new DataOutputStream(p.getOutputStream());
            DataInputStream dis2 = new DataInputStream(p.getInputStream());
            dos2.writeBytes("logcat -b radio -v time\n");
            dos2.flush();
            dos2.writeBytes("exit\n");
            dos2.flush();
            System.currentTimeMillis();
            while (true) {
                String line = dis2.readLine();
                if (line == null) {
                    break;
                }
                this.onLogradioSyncListener.logradioSync(line);
                mDelay(10);
            }
            p.waitFor();
            if (dos2 != null) {
                try {
                    dos2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (dis2 != null) {
                try {
                    dis2.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        } catch (Exception e3) {
            e3.printStackTrace();
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e4) {
                    e4.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Throwable th) {
            if (0 != 0) {
                try {
                    dos.close();
                } catch (IOException e5) {
                    e5.printStackTrace();
                }
            }
            if (0 != 0) {
                try {
                    dis.close();
                } catch (IOException e6) {
                    e6.printStackTrace();
                }
            }
            throw th;
        }
    }

    private void mDelay(long mill) {
        try {
            Thread.sleep(mill);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
